package thread;

/**
 * Created with IntelliJ IDEA
 * Description
 * User：chenshu
 * Date：2024-05-13
 * Time：10:41
 */
class SingleTon {
    private static SingleTon instance = new SingleTon();

    public static SingleTon getInstance() {
        return instance;
    }

    private SingleTon() {}
}

class SingletonLazy {
    volatile private static SingletonLazy instance = null;
    public static SingletonLazy getInstance() {
        if (instance == null) {
            synchronized (SingletonLazy.class) {
                if (instance == null) {
                    //加锁操作
                    instance = new SingletonLazy();
                }
            }
        }
        return instance;
    }

    private SingletonLazy(){}
}

public class Demo17 {
    public static void main(String[] args) {
        SingletonLazy instance1 = SingletonLazy.getInstance();
        SingletonLazy instance2 = SingletonLazy.getInstance();
        System.out.println("是否为同一个实例：" + (instance1 == instance2));
    }
}
